package com.ruix.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

/**
 * Created by hadexs on 16/4/11.
 */
public abstract class ArrayListAdapter<T> extends BaseAdapter {
    protected ArrayList<T> mList;
    protected Context mContext;

    public ArrayListAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    abstract public View getView(int position, View convertView, ViewGroup parent);

    public synchronized void setList(final ArrayList<T> list) {
        ((Activity)mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mList != null) {
                    mList.clear();
                    mList.addAll(list);
                } else {
                    mList = list;
                }
            }
        });

    }

    public ArrayList<T> getList() {
        return mList;
    }

    public void setList(T[] list) {
        if (list != null) {
            ArrayList<T> arrayList = new ArrayList<T>(list.length);
            for (T t : list) {
                arrayList.add(t);
            }
            setList(arrayList);
        }
    }


}
